package cn.zhangxin.pethome.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

public class SentEmailUtil {
    @Autowired
    private static JavaMailSender javaMailSender;
    @Value("${sentEmail}")
    private static String sentEmail;

    public static void toEmail(String titleMsg,String toEmail){
        try {
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom(sentEmail);
            helper.setSubject(titleMsg);
            //设置内容
            helper.setText("<h1>"+titleMsg+"</h1>",true);
            //收件人
            helper.setTo(toEmail);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
